/***************************************************************************
    begin                : jan 01 2013
    copyright            : (C) 2013 by Benoit Valot
    email                : benvalot@gmail.com
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 23 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

package val.lemonde;

import java.util.ArrayList;
import java.util.HashMap;

import android.os.Bundle;
import android.os.Parcelable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class FragmentConfigurationCatOrder extends FragmentLemondeBase
		implements OnItemClickListener, OnClickListener {

	protected ListView maListViewPerso;
	private ArrayList<Categorie> cats;
	protected Parcelable state;
	private ImageView down;
	private ImageView up;
	private int movePosition;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.categorie_order_config,
				container, false);
		maListViewPerso = (ListView) view.findViewById(R.id.listviewperso);
		this.movePosition = 0;
		down = (ImageView) view.findViewById(R.id.catOrder_down);
		down.setOnClickListener(this);
		up = (ImageView) view.findViewById(R.id.catOrder_up);
		up.setOnClickListener(this);

		this.load_content();
		return (view);
	}

	public void load_content() {
		ArrayList<HashMap<String, String>> listItem = new ArrayList<HashMap<String, String>>();
		HashMap<String, String> map;
		// Chargement des catégories
		this.cats = new ArrayList<Categorie>();
		// on ne garde que les catégories select
		for (Categorie cat : this.get_datas().get_categorie(false)) {
			if (cat.isVisible())
				cats.add(cat);
		}

		int i = 0;
		for (Categorie cat : cats) {
			map = new HashMap<String, String>();
			map.put("titre", cat.getName());
			if (i == movePosition)
				map.put("move", "moveCat-true");
			else
				map.put("move", "moveCat-false");
			listItem.add(map);
			i++;
		}

		// Création d'un SimpleAdapter qui se chargera de mettre les items
		// présent dans notre list (listItem) dans la vue affichageitem
		SimpleAdapter mSchedule = new SimpleAdapter(this.getActivity()
				.getBaseContext(), listItem, R.layout.categorie_listes,
				new String[] { "titre", "move" }, new int[] { R.id.cat_title,
						R.id.cat_image });
		// on sauve
		state = maListViewPerso.onSaveInstanceState();

		// on ajoute le viewbinder
		mSchedule.setViewBinder(new BindColorAndImage());

		// On attribut à notre listView l'adapter que l'on vient de créer
		maListViewPerso.setAdapter(mSchedule);

		// Enfin on met un écouteur d'évènement sur notre listView
		maListViewPerso.setOnItemClickListener(this);

		maListViewPerso.onRestoreInstanceState(state);

	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position,
			long arg3) {
		this.movePosition = position;
		this.load_content();
	}

	@Override
	public void onClick(View v) {
		if (v.equals(down) && movePosition != cats.size() - 1) {
			Categorie cat = this.cats.get(movePosition);
			this.cats.remove(movePosition);
			this.cats.add(movePosition + 1, cat);
			movePosition++;
			this.get_datas().save_categorie_order(cats);
			this.load_content();
		} else if (v.equals(up) && movePosition != 0) {
			Categorie cat = this.cats.get(movePosition);
			this.cats.remove(movePosition);
			this.cats.add(movePosition - 1, cat);
			movePosition--;
			this.get_datas().save_categorie_order(cats);
			this.load_content();
		}
	}

}
